package com.srb.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.srb.base.util.JwtUtils;
import com.srb.common.exception.Assert;
import com.srb.common.result.ResponseEnum;
import com.srb.common.util.MD5;
import com.srb.core.mapper.UserAccountMapper;
import com.srb.core.mapper.UserLoginRecordMapper;
import com.srb.core.pojo.entity.UserAccount;
import com.srb.core.pojo.entity.UserInfo;
import com.srb.core.mapper.UserInfoMapper;
import com.srb.core.pojo.entity.UserLoginRecord;
import com.srb.core.pojo.query.UserInfoQuery;
import com.srb.core.pojo.vo.LoginVo;
import com.srb.core.pojo.vo.RegisterVo;
import com.srb.core.pojo.vo.UserInfoVo;
import com.srb.core.service.UserInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 用户基本信息 服务实现类
 * </p>
 *
 * @author dapeng
 * @since 2021-08-08
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {

    @Autowired
    private UserAccountMapper userAccountMapper;

    @Autowired
    private UserLoginRecordMapper userLoginRecordMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void register(RegisterVo registerVo) {
        //判断用户是否被注册
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile",registerVo.getMobile());
        Integer count = baseMapper.selectCount(queryWrapper);
        Assert.isTrue(count == 0, ResponseEnum.MOBILE_EXIST_ERROR);
        //插入用户信息记录：user-info
        UserInfo userInfo = new UserInfo();
        userInfo.setUserType(registerVo.getUserType());
        userInfo.setNickName(registerVo.getMobile());
        userInfo.setName(registerVo.getMobile());
        userInfo.setMobile(registerVo.getMobile());
        userInfo.setPassword(MD5.encrypt(registerVo.getPassword()));
        userInfo.setStatus(UserInfo.STATUS_NORMAL);
        userInfo.setHeadImg(UserInfo.USER_AVATAR);
        baseMapper.insert(userInfo);
        //插入用户账户记录：user-account
        UserAccount userAccount = new UserAccount();
        userAccount.setUserId(userInfo.getId());
        userAccountMapper.insert(userAccount);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public UserInfoVo login(LoginVo loginVo, String ip) {
        //1、用户是否存在
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile",loginVo.getMobile())
                .eq("user_type",loginVo.getUserType());
        UserInfo userInfo = baseMapper.selectOne(queryWrapper);
        Assert.notNull(userInfo,ResponseEnum.LOGIN_MOBILE_ERROR);
        //2、密码是否正确
        Assert.equals(MD5.encrypt(loginVo.getPassword()),userInfo.getPassword(),ResponseEnum.LOGIN_PASSWORD_ERROR);
        //3、用户是否被禁用
        Assert.equals(userInfo.getStatus(),UserInfo.STATUS_NORMAL,ResponseEnum.LOGIN_LOKED_ERROR);
        //4、记录登录日志
        UserLoginRecord userLoginRecord = new UserLoginRecord();
        userLoginRecord.setUserId(userInfo.getId());
        userLoginRecord.setIp(ip);
        userLoginRecordMapper.insert(userLoginRecord);
        //5、生成token
        String token = JwtUtils.createToken(userInfo.getId(), userInfo.getName());
        //6、组装数据
        UserInfoVo userInfoVo = new UserInfoVo();
        userInfoVo.setToken(token);
        userInfoVo.setName(userInfo.getName());
        userInfoVo.setNickName(userInfo.getNickName());
        userInfoVo.setHeadImag(userInfo.getHeadImg());
        userInfoVo.setMobile(userInfo.getMobile());
        userInfoVo.setUserType(userInfo.getUserType());
        return userInfoVo;
    }

    @Override
    public IPage<UserInfo> listPage(Page<UserInfo> pageParam, UserInfoQuery userInfoQuery) {
        if (null == userInfoQuery){
            return baseMapper.selectPage(pageParam,null);
        }
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        String mobile = userInfoQuery.getMobile();
        Integer status = userInfoQuery.getStatus();
        Integer userType = userInfoQuery.getUserType();
        queryWrapper
                .eq(StringUtils.isNotBlank(mobile),"mobile",mobile)
                .eq(null != status,"status",status)
                .eq(null != userType,"user_type",userType);
        return baseMapper.selectPage(pageParam,queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void lock(Long id, Integer status) {
        UserInfo userInfo = new UserInfo();
        userInfo.setId(id);
        userInfo.setStatus(status);
        baseMapper.updateById(userInfo);
    }

    @Override
    public boolean checkMobile(String mobile) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(queryWrapper);
        return count > 0;
    }
}
